package cn.client.ui.view.main.mindfulness

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.paddingFromBaseline
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import cn.client.R
import cn.client.data.remoteData.bean.MindfulnessMedia
import cn.client.data.remoteData.bean.PageSelect
import cn.client.data.userStateManager
import cn.client.navigation.destinations.MindfulnessExercisePage
import coil.compose.AsyncImage


// 收藏
@Composable
fun MindfulnessCollect(
    navController: NavController,
    viewModel: MindfulnessViewModel
) {

    val userIdState = userStateManager.userId.collectAsState(initial = -1)
    val userId = userIdState.value

    // 观察viewModel中的状态
    val mindfulnessCollectState = viewModel.mindfulnessCollect.collectAsState(initial = emptyList())
    val mindfulnessCollect = mindfulnessCollectState.value

    if (userId != -1) {
        viewModel.dispatch(MindfulnessViewModel.PageIntent.GetCollectMindfulnessList(userId))
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
    ) {

        LazyColumn(
            modifier = Modifier
        ) {
            items(mindfulnessCollect) { it: MindfulnessMedia ->
                MindfulnessCollectItem(
                    media = it,
                    onClickMindfulnessCollectItem = {
                        navController.navigate(MindfulnessExercisePage.route + "/${it.id}")
                    }
                )
            }
        }
    }
}


@Composable
fun MindfulnessCollectItem(
    media: MindfulnessMedia,
    onClickMindfulnessCollectItem: () -> Unit
) {
    var isExpanded by remember { mutableStateOf(false) }

    Row(
        modifier = Modifier
            .padding(5.dp)
            .fillMaxWidth()
    ) {
        AsyncImage(
            model = media.imgUrl,
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(88.dp)
                .clip(CircleShape)
                .clickable {
                onClickMindfulnessCollectItem()
                }
        )
        Column(
            modifier = Modifier
                .padding(start = 5.dp)
                .fillMaxWidth()
        ) {
            Text(
                text = media.mediaName,
                modifier = Modifier,
                style = MaterialTheme.typography.bodyMedium,
                fontSize = 20.sp
            )
            Text(
                text = media.mediaInfo,
                modifier = Modifier
                    .fillMaxWidth()
                    .clickable {
                        isExpanded = !isExpanded
                    },
                style = MaterialTheme.typography.bodyMedium,
                maxLines = if (isExpanded) Int.MAX_VALUE else 3,
                color = Color.Gray,
                fontSize = 12.sp
            )
        }

    }

}